package com.dstz.bpm.rest.script;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dstz.base.api.exception.BusinessMessage;
import com.dstz.base.api.response.impl.ResultMsg;
import com.dstz.base.core.util.RestTemplateUtil;
import com.dstz.base.core.util.StringUtil;
import com.dstz.bpm.api.engine.action.cmd.ActionCmd;
import com.dstz.bpm.api.engine.context.BpmContext;
import com.dstz.bpm.api.model.remote.BpmRemoteBusinessData;
import com.dstz.bpm.api.model.remote.FormHandlerResult;
import com.dstz.bpm.engine.action.cmd.DefualtTaskActionCmd;
import com.dstz.sys.api.groovy.IScript;
import com.dstz.sys.api.model.SysIdentity;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.activiti.engine.delegate.VariableScope;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Component;

@Component
public class BpmScriptUtil implements IScript {
   protected Logger LOG = LoggerFactory.getLogger(this.getClass());

   public void httpFormHandler(String url) {
      ActionCmd actionCmd = BpmContext.getActionModel();
      String bizKey = actionCmd.getBusinessKey();
      JSONObject object = actionCmd.getBusData();
      BpmRemoteBusinessData<JSONObject> remoteData = new BpmRemoteBusinessData();
      remoteData.setData(object);
      remoteData.setFlowKey(actionCmd.getBpmInstance().getDefKey());
      remoteData.setInstanceId(actionCmd.getBpmInstance().getId());
      this.LOG.debug("远程表单处理器执行持久化动作：{}", JSON.toJSON(remoteData));
      ParameterizedTypeReference<ResultMsg<FormHandlerResult>> typeReference = new ParameterizedTypeReference<ResultMsg<FormHandlerResult>>() {
      };
      ResultMsg<FormHandlerResult> msg = (ResultMsg)RestTemplateUtil.post(url, remoteData, typeReference);
      this.LOG.debug("远程表单处理器 执行返回结果：{}", JSON.toJSON(msg));
      FormHandlerResult formHanderResult = (FormHandlerResult)ResultMsg.getSuccessResult(msg);
      String bizId = formHanderResult != null ? formHanderResult.getBizId() : "";
      if (StringUtil.isEmpty(bizKey) && StringUtil.isEmpty(bizId)) {
         this.LOG.warn("远程表单处理器执行后未返回业务主键！请确认是不是 没有需要保存的业务数据");
      }

      if (StringUtil.isNotEmpty(bizId)) {
         actionCmd.setBusinessKey(bizId);
      }

      if (formHanderResult != null && CollectionUtil.isNotEmpty(formHanderResult.getVariables())) {
         actionCmd.setActionVariables(formHanderResult.getVariables());
      }

   }

   public void persistenceFutureNodeUserSetting(VariableScope variableScope, String nodeId) {
      DefualtTaskActionCmd actionCmd = (DefualtTaskActionCmd)BpmContext.getActionModel();
      List<SysIdentity> nodeUserSetting = actionCmd.getBpmIdentity(nodeId);
      if (CollectionUtil.isEmpty(nodeUserSetting)) {
         throw new BusinessMessage("请指定下一环节候选人！");
      } else {
         variableScope.setVariable("futureUserSetting_" + nodeId, nodeUserSetting);
      }
   }

   public Set<SysIdentity> getVariablesUserSetting(VariableScope variableScope, String nodeId) {
      if (variableScope == null) {
         return null;
      } else {
         List<SysIdentity> nodeUserSetting = (List)variableScope.getVariable("futureUserSetting_" + nodeId);
         if (CollectionUtil.isEmpty(nodeUserSetting)) {
            this.LOG.warn("未解析到流程变量人员！ 节点：futureUserSetting_{} ", nodeId);
            return Collections.EMPTY_SET;
         } else {
            return new HashSet(nodeUserSetting);
         }
      }
   }
}
